﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RoyTools.D3Monitor.FormUI.Helper;

namespace RoyTools.D3Monitor.FormUI
{
    public partial class Setup : Form
    {
        /// <summary>
        /// 配置
        /// </summary>
        private Config config { get; set; }

        public Setup()
        {
            InitializeComponent();
        }

        private void Setup_Load(object sender, EventArgs e)
        {
            ConfigHelper.LoadConfig();
            this.config = Program.Config;
            LoadLogSettings();
            LoadMailSettings();
        }

        /// <summary>
        /// 浏览日志目录事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowserDirectory_Click(object sender, EventArgs e)
        {
            var folderBrowserDialog = new FolderBrowserDialog();
            var result = folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtLogDirectory.Text = folderBrowserDialog.SelectedPath;
            }
        }

        /// <summary>
        /// 新建日志事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtName.Text) && !string.IsNullOrWhiteSpace(txtLogDirectory.Text) && !config.LogMonitorSettings.Exists(l => l.Name.Equals(txtName.Text.Trim(), StringComparison.CurrentCultureIgnoreCase)))
            {
                var logMonitorSetting = new LogMonitorSetting();
                logMonitorSetting.Name = txtName.Text.Trim();
                logMonitorSetting.LogDirectory = txtLogDirectory.Text.Trim();
                config.LogMonitorSettings.Add(logMonitorSetting);
                LoadLogSettings();
                LogTextClear();
            }
        }

        /// <summary>
        /// 清空日志文本事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            LogTextClear();
        }

        /// <summary>
        /// 删除日志事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            config.LogMonitorSettings.RemoveAll(l => l.LogDirectory.Equals(txtLogDirectory.Text.Trim(), StringComparison.CurrentCultureIgnoreCase));
            LoadLogSettings();
        }

        /// <summary>
        /// 日志列表选择项变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void lsvLog_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsvLog.SelectedItems.Count > 0)
            {
                var selectedListViewItem = lsvLog.SelectedItems[0];
                txtName.Text = selectedListViewItem.SubItems[0].Text;
                txtLogDirectory.Text = selectedListViewItem.SubItems[1].Text;
            }
        }

        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            SaveMailSettings();
            Program.Config = config;
            ConfigHelper.SaveConfig();
            MonitorManager.StopLogMonitor();
            MonitorManager.SetLogMonitor();
            FormHelper.Close(this);
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            FormHelper.Close(this);
        }

        /// <summary>
        /// 日志文本清空
        /// </summary>
        private void LogTextClear()
        {
            txtName.Clear();
            txtLogDirectory.Clear();
        }

        /// <summary>
        /// 载入日志设置
        /// </summary>
        private void LoadLogSettings()
        {
            lsvLog.BeginUpdate();
            lsvLog.Items.Clear();
            if (config.LogMonitorSettings == null)
            {
                config.LogMonitorSettings = new List<LogMonitorSetting>();
            }
            config.LogMonitorSettings.ForEach(l =>
            {
                var listViewItem = new ListViewItem(new string[] { l.Name, l.LogDirectory });
                lsvLog.Items.Add(listViewItem);
            });
            lsvLog.EndUpdate();
        }

        /// <summary>
        /// 载入邮件设置
        /// </summary>
        private void LoadMailSettings()
        {
            if (config.MailSetting == null)
            {
                config.MailSetting = new MailSetting();
            }
            txtTo.Text = config.MailSetting.To;
            nudSendInterval.Value = config.MailSetting.SendInterval;
            txtSmtp.Text = config.MailSetting.Smtp;
            txtPort.Text = config.MailSetting.Port.ToString();
            txtAccount.Text = config.MailSetting.Account;
            txtPassword.Text = config.MailSetting.Password;
        }

        /// <summary>
        /// 保存邮件设置
        /// </summary>
        private void SaveMailSettings()
        {
            config.MailSetting.To = txtTo.Text.Trim();
            config.MailSetting.SendInterval = (int)(nudSendInterval.Value <= 0 ? 1 : nudSendInterval.Value);
            config.MailSetting.Smtp = txtSmtp.Text.Trim();
            try
            {
                config.MailSetting.Port = int.Parse(txtPort.Text.Trim());
            }
            catch
            {
                config.MailSetting.Port = 25;
            }
            config.MailSetting.Account = txtAccount.Text.Trim();
            config.MailSetting.From = config.MailSetting.Account;
            config.MailSetting.Password = txtPassword.Text.Trim();
        }

    }
}
